part of base_package;

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

abstract class MainBase extends StatelessWidget with WidgetsBindingObserver {
  const MainBase({Key? key}) : super(key: key);

  get clipboardDataText => null;

  @override
  Widget build(BuildContext context) {
    ScreenDefault screenDefault = setDefaultWH();
    HttpOverrides.global = CjyHttpOverrides();
    WidgetsBinding.instance.addObserver(this);
    if (!BaseGlobal.isMainInitData) {
      BaseGlobal.isMainInitData = true;
      initData();
    }
    initBuild();
    return ScreenUtilInit(
      designSize: Size(screenDefault.width, screenDefault.height),
      builder: (BuildContext context, Widget? widget) => MaterialApp(
        navigatorKey: navigatorKey,
        localizationsDelegates: localizationsDelegates(),
        supportedLocales: const [
          Locale('zh', 'CN'),
          Locale('en', 'US'),
        ],
        title: setTitle(),
        //注册路由表
        initialRoute: '/',
        routes: {
          '/': (context) => setStartPage(context),
        },
        builder: (BuildContext context, Widget? child) {
          /// make sure that loading can be displayed in front of all other widgets
          Widget page = setPageContent(context, child);
          return FlutterEasyLoading(
            child: MediaQuery(
              ///设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: page,
            ),
          );
        },
      ),
    );
  }

  /// 设置全局页面，child也要加入到页面中
  Widget setPageContent(BuildContext context, Widget? child) {
    return Net.isShowLogBtn
        ? Stack(
            children: [
              child ?? Container(),
              Positioned(
                left: 375 / 2 - 20,
                child: GestureDetector(
                  onTap: () {
                    Event.eventBus.fire(Event(Net.EVENT_NetLogPage, ''));
                  },
                  child: Container(
                    margin: const EdgeInsets.only(top: 20),
                    color: Colors.red,
                    alignment: Alignment.center,
                    width: 40,
                    height: 40,
                    child: const Text(
                      'LOG',
                      style: TextStyle(color: Colors.white, fontSize: 18),
                    ),
                  ),
                ),
              ),
            ],
          )
        : child ?? Container();
  }

  /// APP启动进入的页面
  Widget setStartPage(BuildContext context);

  /// APP标题
  String setTitle();

  /// 设置UI设计稿尺寸
  ScreenDefault setDefaultWH();

  /// 初始化数据，只会调用一次
  initData();

  /// 初始化数据，会根据页面刷新调用多次
  initBuild() {}

  /// 国际化处理
  Iterable<LocalizationsDelegate<dynamic>> localizationsDelegates();

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.paused) {
      appBackstage();
    }
    if (state == AppLifecycleState.resumed) {
      appFrontdesk();
    }
  }

  /// APP切换到后台的回调
  void appBackstage() {}

  /// APP切换到前台的回调
  void appFrontdesk() {
    Event.eventBus
        .fire(Event(PermissionUtil.getInstance.EVENT_PATROL_PERMISSION, ''));
    Event.eventBus.fire(Event(PermissionUtil.getInstance.EVENT_PERMISSION, ''));
  }
}
